#!/bin/sh
#
#  MKSRC -- Make a pure source tree and configure the links for the 
#  current architecture.

set -e

# Initialize the $iraf and environment.
if [ -z "$iraf" ]; then
  if [ -e "$HOME/.iraf/setup.sh" ]; then
    . "$HOME/.iraf/setup.sh"
  else
    . unix/hlib/setup.sh
  fi
else
    . "$iraf/unix/hlib/setup.sh"
fi


# Utility aliases.
. "$iraf/unix/hlib/util.sh"

if [ -z "$iraf" ]; then
    echo
    echo "Error:  You must have the iraf env variable defined !"
    echo
    exit 1
fi


exec=yes

#=============================================================================
# Process any command line arguments.
#=============================================================================
while [ -n "$1" ] ; do
    case "$1" in
    "-n")                            # no execute
        exec=no
	;;
    *)
        ERRMSG  "$0: unknown argument $1"
	;;
    esac

    if [ -n "$2" ]; then
        shift
    else
        break
    fi
done



#----------------------------------
# Determine platform architecture.
#----------------------------------

echo "Clean host directories ...."
  if [ $exec = "yes" ]; then
    rm -f unix/bin.*/*.e unix/bin.*/lib*.a
  else
    ls -l unix/bin.*/*.e unix/bin.*/lib*.a
  fi


echo "Clean src directories ...."
dirs="local math pkg sys noao"
for i in $dirs; do
  if [ $exec = "yes" ]; then
    find "$i" -type f -name \*.a   -exec rm -f {} \;
    find "$i" -type f -name \*.e   -exec rm -f {} \;
    find "$i" -type f -name \*.o   -exec rm -f {} \;
    find "$i" -type f -name OBJS\* -exec ls -l {} \;
  else
    find "$i" -type f -name \*.a   -exec ls -l {} \;
    find "$i" -type f -name \*.e   -exec ls -l {} \;
    find "$i" -type f -name \*.o   -exec ls -l {} \;
    find "$i" -type f -name OBJS\* -exec ls -l {} \;
  fi
done


echo "Clean bin directories ...."
if [ $exec = "yes" ]; then
    rm -rf bin.*/*.[aeoZ] noao/bin.*/*.[aeoZ] OBJS*
else
    ls -l  bin.*/*.[aeoZ] noao/bin.*/*.[aeoZ] OBJS*
fi


echo "Cleaning spool files ...."
if [ $exec = "yes" ]; then
    find . -name spool\* -print -exec rm -f {} \;
    find . -name _spool\* -print -exec rm -f {} \;
else
    find . -name spool\* -exec ls {} \;
    find . -name _spool\* -exec ls {} \;
fi
